package org.datacoder.geocoding.datastructures.intervals

import org.datacoder.geocoding.*
import org.datacoder.geocoding.datastructures.geoshapes.*
import org.datacoder.geocoding.datastructures.points.*
import org.datacoder.geocoding.datastructures.rectangles.*
import org.datacoder.geocoding.jurisdiction.*

class PairedIntervalRangeTrees implements IntersectingIntervalsFinder {

    private IntervalTree intervalTree
    private RangeTree rangeTree
    int type

    public PairedIntervalRangeTrees(Collection<Jurisdiction> jurisdictions, int type){
        this.type = type
        intervalTree = new IntervalTree(jurisdictions, type)
        rangeTree = new RangeTree(jurisdictions, GeoShape.getOtherType(type))
    }

    public Set<Jurisdiction> findIntersectingIntervals(GeoInterval gi){
        Set<Jurisdiction> intervalTreeJurisdictions = intervalTree.findIntervalsContainingPoint(gi.retrieveSingleValue())
        Set<Jurisdiction> rangeTreeJurisdictions = rangeTree.findPointsWithinInterval(gi.retrieveMinDoubleValue(), gi.retrieveMaxDoubleValue())
        intervalTreeJurisdictions.retainAll(rangeTreeJurisdictions)
        return intervalTreeJurisdictions
    }

}

